<?php 
	require_once("init.php"); 
?>
  
<div id="myprofile" title="My Profile" class="panel">
  <h2><?php print $_SESSION["username"]; ?>'s Statistics</h2>
  <fieldset>
    <div class="row">
    	<?php 
				$qProfile = mysql_query("
					SELECT count(voteId) AS totalCount
					FROM votes
					WHERE userId = ".$_SESSION["userId"]."
				");
				$profile_totalVotes = mysql_fetch_array($qProfile);
				$profile_totalVotes = $profile_totalVotes["totalCount"];
			?>
      <label>Total Votes: <?php print $profile_totalVotes; ?></label>
    </div>
    <div class="row">
    	<?php 
				$qProfile = mysql_query("
					SELECT count(DISTINCT v.voteId) AS totalCount
					FROM votes v
					WHERE
						v.userId = ".$_SESSION["userId"]."
						AND v.answer = (
							SELECT
								IF (t1.answer1Votes > t1.answer2Votes, 1, IF (t1.answer1Votes < t1.answer2Votes, 2, 0))
							FROM topics t1
							WHERE t1.topicId = v.topicId
								AND now() > t1.dateClosed
								AND t1.points > 0
						)
				");
				$profile_majorityVotes = mysql_fetch_array($qProfile);
				$profile_majorityVotes = $profile_majorityVotes["totalCount"];
			?>
      <label>Votes in Majority: <?php print $profile_majorityVotes; ?></label>
    </div>
    <div class="row">
    	<?php 
				if ($profile_majorityVotes > 0) {
					$profile_accuracy = $profile_majorityVotes/$profile_totalVotes*100;
				} else {
					$profile_accuracy = "N/A";
				}
			?>
      <label>Stereotype: <?php print $profile_accuracy; if ($profile_accuracy != "N/A") print "%"; ?></label>
    </div>
    <div class="row">
    	<?php 
				$qProfile = mysql_query("
					SELECT count(topicId) AS totalCount
					FROM topics
					WHERE userId = ".$_SESSION["userId"]."
				");
				$profile_submittedTopics = mysql_fetch_array($qProfile);
				$profile_submittedTopics = $profile_submittedTopics["totalCount"];
			?>
      <label>Submitted Topics: <?php print $profile_submittedTopics; ?></label>
    </div>
    <div class="row">
    	<?php 
				$qProfile = mysql_query("
					SELECT count(DISTINCT topicId) AS totalCount
					FROM moderations
					WHERE userId = ".$_SESSION["userId"]."
				");
				$profile_moderatedTopics = mysql_fetch_array($qProfile);
				$profile_moderatedTopics = $profile_moderatedTopics["totalCount"];
			?>
      <label>Moderated Topics: <?php print $profile_moderatedTopics; ?></label>
    </div>
    <div class="row">
    	<?php 
				$qProfile = mysql_query("
					SELECT points
					FROM users
					WHERE userId = ".$_SESSION["userId"]."
				");
				$profile_points = mysql_fetch_array($qProfile);
				$profile_points = $profile_points["points"];
			?>
      <label>Accumulated Points: <?php print $profile_points; ?></label>
    </div>
    <div class="row">
    	<?php 
				$profile_rank = "Member"; 
				if ($profile_points < 150000) $profile_rank = "Supreme Leader";
				if ($profile_points < 100000) $profile_rank = "Veteran Leader";
				if ($profile_points < 75000) $profile_rank = "Advanced Leader";
				if ($profile_points < 50000) $profile_rank = "Leader";
				if ($profile_points < 25000) $profile_rank = "Supreme Contributor";
				if ($profile_points < 10000) $profile_rank = "Veteran Contributor";
				if ($profile_points < 7500) $profile_rank = "Advanced Contributor";
				if ($profile_points < 5000) $profile_rank = "Active Contributor";
				if ($profile_points < 2500) $profile_rank = "Contributor";
				if ($profile_points < 1000) $profile_rank = "Supreme Member";
				if ($profile_points < 500) $profile_rank = "Veteran Member";
				if ($profile_points < 250) $profile_rank = "Advanced Member";
				if ($profile_points < 100) $profile_rank = "Active Member";
				if ($profile_points < 50) $profile_rank = "Member";
			?>
      <label>Rank: <?php print $profile_rank; ?></label>
    </div>
  </fieldset>
</div>